package com.powernode.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.SysUser;
import com.powernode.dto.SysUserQueryParam;
import com.powernode.model.Result;
import com.powernode.service.SysUserService;
import com.powernode.utils.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "管理员接口管理")
@RequestMapping("sys/user")
@RestController
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @ApiOperation("查询登录的用户信息")
    @GetMapping("info")
    public Result<SysUser> loadSysUserInfo() {
        //获取登录用户标识
        Long userId = AuthUtils.getLoginUserId();
        //根据用户标识查询登录用户信息
        SysUser sysUser = sysUserService.getById(userId);
        return Result.success(sysUser);
    }


    // 多条件分页查询管理员列表
    // 该接口通过GET请求接收分页参数，支持根据用户名等条件进行查询
    // 权限控制：需要具有系统用户分页查询的权限
    @ApiOperation("多条件分页查询管理员列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public Result<Page<SysUser>> loadSysUserPage(Page<SysUser> page, SysUserQueryParam queryParam) {
        // 使用Lambda表达式构建查询条件，如果用户名不为空则进行模糊查询，并按创建时间降序排序
        page = sysUserService.page(page, new LambdaQueryWrapper<SysUser>()
                .like(StringUtils.hasText(queryParam.getUsername()), SysUser::getUsername, queryParam.getUsername())
                .orderByDesc(SysUser::getCreateTime)
        );
        // 返回查询结果，表示操作成功
        return Result.success(page);
    }

    /**
     * 新增管理员
     *
     * @param sysUser
     * @return
     */
    @ApiOperation("新增管理员")
    @PostMapping
    // 使用PreAuthorize注解指定该方法的访问权限，只有具备'sys:user:save'权限的用户才能执行此操作
    @PreAuthorize("hasAuthority('sys:user:save')")
    // 定义一个用于新增管理员的接口方法
    public Result<String> saveSysUser(@RequestBody SysUser sysUser) {
        // 调用sysUserService的saveSysUser方法，尝试新增管理员，返回受影响的行数
        Integer count = sysUserService.saveSysUser(sysUser);
        // 根据插入结果的行数判断操作是否成功，并返回相应的结果对象
        return Result.handle(count > 0);
    }

    @ApiOperation("根据标识查询系统管理员详细信息")
    @GetMapping("info/{id}")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserDetail(@PathVariable Long id) {
        SysUser sysUser = sysUserService.querySysUserInfoByUserId(id);
        return Result.success(sysUser);
    }

    @ApiOperation("修改管理员信息")
    @PutMapping
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<String> modifySysUserInfo(@RequestBody SysUser sysUser) {
        Integer count = sysUserService.modifySysUserInfo(sysUser);
        return Result.handle(count > 0);
    }

    @ApiOperation("批量/单个删除管理员")
    @DeleteMapping("{userIds}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<String> removeSysUser(@PathVariable List<Long> userIds) {
      Boolean removed =  sysUserService.removeSysUsersByUserIdList(userIds);
        return Result.handle(removed);
    }
}
